package com.sky.service.impl;

import com.sky.context.BaseContext;
import com.sky.dto.ShoppingCartDTO;
import com.sky.entity.Dish;
import com.sky.entity.Setmeal;
import com.sky.entity.ShoppingCart;
import com.sky.mapper.DishMapper;
import com.sky.mapper.SetmealMapper;
import com.sky.mapper.UserShoppingCartMapper;
import com.sky.service.UserShoppingCartService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.Collections;
import java.util.List;


@Service
public class UserShoppingCartServiceImpl implements UserShoppingCartService {

    @Autowired
    private UserShoppingCartMapper userShoppingCartMapper;
    @Autowired
    private DishMapper dishMapper;
    @Autowired
    private SetmealMapper setmealMapper;


    @Override
    public void insert(ShoppingCartDTO shoppingCartDTO) {
        ShoppingCart shoppingCart = new ShoppingCart();
        BeanUtils.copyProperties(shoppingCartDTO, shoppingCart);
        shoppingCart.setUserId(BaseContext.getCurrentId());

        // 如果查询到了数据就将数量加一
        List<ShoppingCart> shoppingCartList = userShoppingCartMapper.list(shoppingCart);

        // 如果添加的是菜品，且菜品的口味不一致。则新加一条数据而不是数量加一。
        if (shoppingCartList != null && !shoppingCartList.isEmpty()) {
            shoppingCart = shoppingCartList.get(0);
            shoppingCart.setNumber(shoppingCart.getNumber() + 1);
            userShoppingCartMapper.update(shoppingCart);
            return;
        }

        // 没有查询到数据就，新增数据。
        Long dishId = shoppingCart.getDishId();
        if (dishId != null) {
            Dish dish = dishMapper.getDish(dishId);

            shoppingCart.setImage(dish.getImage());
            shoppingCart.setAmount(dish.getPrice());
            shoppingCart.setName(dish.getName());
            shoppingCart.setDishFlavor(shoppingCartDTO.getDishFlavor());
        } else {
            Setmeal setmeal = setmealMapper.getById(shoppingCart.getSetmealId());

            shoppingCart.setImage(setmeal.getImage());
            shoppingCart.setAmount(setmeal.getPrice());
            shoppingCart.setName(setmeal.getName());
        }

        shoppingCart.setNumber(1);
        shoppingCart.setCreateTime(LocalDateTime.now());
        userShoppingCartMapper.save(shoppingCart);
    }

    @Override
    public List<ShoppingCart> list() {
        Long currentId = BaseContext.getCurrentId();
        ShoppingCart shoppingCart = new ShoppingCart();
        shoppingCart.setUserId(currentId);

        return userShoppingCartMapper.list(shoppingCart);
    }

    @Override
    public void delete(ShoppingCartDTO shoppingCartDTO) {
        // 查询菜品或套餐是否大于1份，如果大于就减去一份
        ShoppingCart shoppingCart = new ShoppingCart();
        BeanUtils.copyProperties(shoppingCartDTO, shoppingCart);
        shoppingCart.setUserId(BaseContext.getCurrentId());
        List<ShoppingCart> shoppingCartList = userShoppingCartMapper.list(shoppingCart);
        if (shoppingCartList != null && !shoppingCartList.isEmpty()) {
            shoppingCart = shoppingCartList.get(0);
            if (shoppingCart.getNumber() > 1) {
                shoppingCart.setNumber(shoppingCart.getNumber() - 1);
                userShoppingCartMapper.update(shoppingCart);
                return;
            }
            // 如果小于等于就删除该菜品或套餐
            userShoppingCartMapper.delete(shoppingCart);
        }
    }

    @Override
    public void clean() {
        ShoppingCart shoppingCart = new ShoppingCart();
        shoppingCart.setUserId(BaseContext.getCurrentId());

        userShoppingCartMapper.delete(shoppingCart);
    }

}
